package com.zhang.crm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.crm.constant.Constants;
import com.zhang.crm.listener.UploadDataListener;
import com.zhang.crm.mapper.TClueRemarkmapper;
import com.zhang.crm.mapper.TCluemapper;
import com.zhang.crm.mapper.TUsermapper;
import com.zhang.crm.pojo.TClue;
import com.zhang.crm.pojo.TUser;
import com.zhang.crm.service.ClueService;
import com.zhang.crm.util.UserUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Resource
    private TCluemapper tCluemapper;
    @Resource
    private TUsermapper tUsermapper;

    @Resource
    private TClueRemarkmapper tClueRemarkmapper;
    @Override
    public PageInfo<TClue> getClueListByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_DATA_SIZE);
        List<TClue> tClueList = tCluemapper.selectClueList();
        PageInfo<TClue> tCluePageInfo = new PageInfo<>(tClueList);
        return tCluePageInfo;
    }

    @Override
    public Boolean addService(TClue tClue) {
        tClue.setCreateTime(new Date());
        tClue.setCreateBy(UserUtil.getUserInfo().getId());
        int result = tCluemapper.insert(tClue);
        return result > 0 ? true : false;
    }

    @Override
    public TClue getClue(Integer id) {
        TClue tClue = tCluemapper.selectClueById(id);
        //获取创建人
        if (tClue.getCreateBy() != null){
            TUser tUser = tUsermapper.selectByPrimaryKey(tClue.getCreateBy());
            tClue.setCreateName(tUser.getName());
        }
        //获取编辑人
        if (tClue.getEditBy() != null){
            TUser tUser = tUsermapper.selectByPrimaryKey(tClue.getEditBy());
            tClue.setEditName(tUser.getName());
        }
        return tClue;
    }

    @Override
    public Boolean updateClueRemark(TClue tClue) {
        int i = tCluemapper.updateByPrimaryKeySelective(tClue);
        return i > 0 ? true : false;
    }

    @Override
    @Transactional
    public Boolean delActivityById(Integer clueId) {
        int i = tCluemapper.deleteByPrimaryKey(clueId);
        //删除此条线索的所有备注
        int r = tClueRemarkmapper.upDateClueDelete(clueId);

        return i > 0 ? true : false;
    }

    @Override
    @Transactional
    public Boolean delClueBatch(List<String> ids) {
        int i = tCluemapper.deleteClueBatch(ids);
        return i > 0 ? true : false;
    }

    /**
     *  上传的读取Excel文件
     * @param inputStream 文件流
     */
    @Override
    public void importExcel(InputStream inputStream) {
        //EasyExcel解析
        //每读一行触发监听器
        EasyExcel.read(inputStream, TClue.class, new UploadDataListener(tCluemapper))
                .sheet()
                .doRead();
    }

    @Override
    public Integer update(TClue tClue) {
        return tCluemapper.updateByPrimaryKeySelective(tClue);
    }

    @Override
    public PageInfo<TClue> getCustomerListByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_DATA_SIZE);
        List<TClue> tClueList = tCluemapper.selectCustomerList();
        PageInfo<TClue> tCluePageInfo = new PageInfo<>(tClueList);
        return tCluePageInfo;
    }

    @Override
    public TClue getClueCustomer(Integer id) {
        TClue tClue = tCluemapper.selectClueCustomerById(id);
        //获取创建人
        if (tClue.getCreateBy() != null){
            TUser tUser = tUsermapper.selectByPrimaryKey(tClue.getCreateBy());
            tClue.setCreateName(tUser.getName());
        }
        //获取编辑人
        if (tClue.getEditBy() != null){
            TUser tUser = tUsermapper.selectByPrimaryKey(tClue.getEditBy());
            tClue.setEditName(tUser.getName());
        }
        return tClue;
    }
}
